﻿-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE FUNCTION FUNCTIONLIST 
(
	-- Add the parameters for the function here
	@EntityID int
)
RETURNS 
TABLE
AS
RETURN (
SELECT * FROM
(
SELECT    DISTINCT    dbo.DefinedFunctions.FunctionName, dbo.DefinedFunctions.FunctionCommand, dbo.DefinedFunctions.ViewName, dbo.DefinedFunctions.EditAttributes, dbo.DefinedFunctions.DisplayAttributeName, 
                         CASE dbo.DefinedFunctionRelationships.Forward WHEN 1 THEN Entity1ID ELSE Entity2ID END AS EntityID
FROM            dbo.DefinedFunctionRelationships INNER JOIN
                         dbo.DefinedFunctions ON dbo.DefinedFunctionRelationships.FunctionID = dbo.DefinedFunctions.FunctionID INNER JOIN
                         dbo.EntityRelationships ON dbo.DefinedFunctionRelationships.RelationshipShortName = dbo.EntityRelationships.ShortRelationshipName
WHERE        (dbo.ISCURRENT(dbo.EntityRelationships.Entity1ID, dbo.EntityRelationships.ShortRelationshipName, dbo.EntityRelationships.Entity2ID, NULL) = 1)
) AS T
WHERE
				T.EntityID = @EntityID
)